module Boids.Utilities

open Boids;;
open System;;
open Microsoft.Xna.Framework;;      


let rand = new Random();;


let rand_single(max:single) = 
  match max with
  | _ when max >= 1.f ->
    Convert.ToSingle(rand.Next(Convert.ToInt32(max) - 1)) + Convert.ToSingle(rand.NextDouble());
    
  | _ ->
    Convert.ToSingle(rand.NextDouble());;
  
  
let rand_single_range(max:single) =
  match rand.Next(20) with
  | _ as x when x % 2 <> 0 ->
      -rand_single(max);
      
  | _ -> 
      rand_single(max);;
    

let heading (v:Vector2) = 
  -1.0f * Convert.ToSingle(System.Math.Atan2(Convert.ToDouble(v.Y) * -1., Convert.ToDouble(v.X)));;
    

let vector_init() = new Vector2();;


let generate_boid_list count phys (settings:BoidSettings) = 
  [for x in 1..count -> 
    let b = new Boid(phys, settings) in
    b.Position <- new Vector2(rand_single(settings.width - settings.wall) + settings.wall, rand_single(settings.height - settings.wall) + settings.wall);
    b];;
    
let boid_sum_list (bl:(Boid list)) = 
  List.fold_left(+) {
    pos = new Vector2();
    lin_vel = new Vector2(); } bl;;


let vector_limit (v:Vector2) limit =
  match v.Length() with
  | _ as m when m > limit ->
    v.Normalize();
		(new Vector2(v.X * limit, v.Y * limit));

	| _ ->
	  v;;